﻿using System;
using System.Data.SqlClient;
using TAR4_Lib.General;

namespace TAR4_Lib.Restaurant.Delivery
{
    public class DeliverySite
    {
        #region Costructor
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.Object"/> class.
        /// </summary>
        private DeliverySite(int deliverySiteID, string name, string description, DeliveryModeCollection deliveryModes, int maxTicketsPerLetter)
        {
            DeliverySiteID = deliverySiteID;
            Name = name;
            Description = description;
            DeliveryModes = deliveryModes;
            MaxTicketsPerLetter = maxTicketsPerLetter;
        }
        #endregion

        #region Static constructors
        /// <summary>
        /// Get a delivery site from its identifier
        /// </summary>
        /// <param name="deliverySiteID"></param>
        /// <exception cref="ApplicationException">Delivery site not found</exception>
        /// <returns></returns>
        public static DeliverySite GetByID(int deliverySiteID)
        {
            DeliverySite retValue = null;
            using (SqlConnection conn = new SqlConnection(Settings.GetDBConnectionString()))
            {
                using (SqlCommand cmd = new SqlCommand()
                                    {
                                        Connection = conn,
                                        CommandText = "[Restaurant].[SP_DeliverySite_S]",
                                        CommandType = System.Data.CommandType.StoredProcedure
                                    })
                {
                    cmd.Parameters.Add(new SqlParameter("@DeliverySiteID", System.Data.SqlDbType.Int) { Value = deliverySiteID });

                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        try
                        {
                            if (dr.Read())
                            {
                                retValue = new DeliverySite(
                                    (int)dr["DeliverySiteID"],
                                    (string)dr["Name"],
                                    dr["Description"] == DBNull.Value ? null : (string)dr["Description"],
                                    null, // Valorizzati in seguito
                                    (int)dr["MaxTicketsPerLetter"]);
                            }
                            else
                            {
                                throw new ApplicationException(string.Format("DeliverySite {0} not found", deliverySiteID));
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
            }

            // Aggancia l'elenco dei DeliverySiteMode associati
            retValue.DeliveryModes = DeliveryModeCollection.GetAllAvailableDeliveryModeForDeliverySite(deliverySiteID);

            return retValue;
        }

        public static DeliverySite SaveNewDeliverySite(string name, string description, DeliveryModeCollection deliveryModes, int maxTicketPerLetter)
        {
            // TODO: implement SaveOnDB here
            const int deliverySiteID = 1;
            return new DeliverySite(deliverySiteID, name, description, deliveryModes, maxTicketPerLetter);
        }
        #endregion

        #region Properties
        /// <summary>
        /// Unique identifier of the Delivery Site
        /// </summary>
        public int DeliverySiteID { get; protected set; }

        /// <summary>
        /// Friendly name of the Delivery Site
        /// </summary>
        public string Name { get; protected set; }

        /// <summary>
        /// Complete description of the Delivery Site
        /// </summary>
        public string Description { get; protected set; }

        /// <summary>
        /// Available delivery modes of the Delivery Site
        /// </summary>
        public DeliveryModeCollection DeliveryModes { get; protected set; }

        /// <summary>
        /// Numbers of tickets per letter (e.g. A01-A02...A50)
        /// </summary>
        public int MaxTicketsPerLetter { get; protected set; }
        #endregion

        #region Methods
        public ProductCollection GetAssociatedProducts()
        {
            throw new NotImplementedException();
        }

        public DeliverySiteRunningValues GetRunningValues(DateTime startDateTime, DateTime endDateTime)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
